package com.ctshk.app.order.scenic.controller;

import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.model.AppReq;
import com.ctshk.common.web.support.BaseController;
import com.ctshk.rpc.order.scenic.dto.app.ScenicOrderAppOrderDetailDTO;
import com.ctshk.rpc.order.scenic.dto.app.ScenicOrderPayDTO;
import com.ctshk.rpc.order.scenic.req.app.ScenicOrderAppPlaceOrderReq;
import com.ctshk.rpc.order.scenic.req.app.ScenicOrderCallPayReq;
import com.ctshk.rpc.order.scenic.req.app.ScenicOrderConsumeReq;
import com.ctshk.rpc.order.scenic.service.app.IScenicOrderAppService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;

@Api(value = "景点门票-APP", tags = "景点门票-APP")
@RestController
@RequestMapping("/app")
public class ScenicOrderAppController extends BaseController {
    @DubboReference
    IScenicOrderAppService iScenicOrderAppService;

    @PostMapping("/addOrderInfo")
    @ApiOperation(value = "提交订单", notes = "提交订单")
    public Result<Long> addOrderInfo(@RequestBody ScenicOrderAppPlaceOrderReq req, AppReq appReq){
        req.setUserId(getCurrentUserAllowNotLogInUserId());
        return iScenicOrderAppService.addOrderInfo(req, appReq);
    }

    @DeleteMapping("/cancelOrder/{orderId}")
    @ApiOperation(value = "取消订单", notes = "取消订单")
    public Result<Boolean> cancelOrder(@PathVariable("orderId") Long orderId){
        return iScenicOrderAppService.cancelOrder(orderId);
    }

    @GetMapping("/selectOrderInfo/{orderId}")
    @ApiOperation(value = "查询订单详情", notes = "查询订单详情")
    public Result<ScenicOrderAppOrderDetailDTO> selectOrderInfo(@PathVariable("orderId") Long orderId){
        return iScenicOrderAppService.selectOrderInfo(orderId);
    }

    @GetMapping("/selectOrderPayInfo/{orderId}")
    @ApiOperation(value = "查询订单付款信息", notes = "查询订单付款信息")
    public Result<ScenicOrderPayDTO> selectOrderPayInfo(@PathVariable("orderId") Long orderId){
        return iScenicOrderAppService.selectOrderPayInfo(orderId);
    }

    @PostMapping("/callPay")
    @ApiOperation(value = "唤起支付", notes = "唤起支付")
    public void callPay(ScenicOrderCallPayReq req, HttpServletResponse response){
        Result result = iScenicOrderAppService.callPay(req);
        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");
        try {
            response.getWriter().write(String.valueOf(result.getData()));
            response.getWriter().flush();
            response.getWriter().close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "支付回调", notes = "支付回调")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "景点门票-支付订单", operDesc = "景点门票-支付订单")
    @PostMapping("/payment")
    public Result payment(@RequestBody @Valid ScenicOrderConsumeReq scenicOrderConsumeReq) {
        return iScenicOrderAppService.payment(scenicOrderConsumeReq);
    }
}
